<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Template Functions</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="plugins.html" title="Chapter 18. Extending Smarty With Plugins">
<link rel="prev" href="plugins.writing.html" title="Writing Plugins">
<link rel="next" href="plugins.modifiers.html" title="Modifiers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Template Functions</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="plugins.writing.html">Prev</a> </td>
<th width="60%" align="center">Chapter 18. Extending Smarty With Plugins</th>
<td width="20%" align="right"> <a accesskey="n" href="plugins.modifiers.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Template Functions">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="plugins.functions"></a>Template Functions</h2></div></div></div>
<div class="funcsynopsis">
<table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table">
<tr>
<td><code class="funcdef">void <b class="fsfunc">smarty_function_name</b>(</code></td>
<td>
<var class="pdparam">$params</var>, </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<var class="pdparam">$template</var><code>)</code>;</td>
<td> </td>
</tr>
</table>
<div class="paramdef-list">
<code>array <var class="pdparam">$params</var></code>;<br><code>object <var class="pdparam">$template</var></code>;</div>
<div class="funcprototype-spacer"> </div>
</div>
<p>
     All <a class="link" href="language.syntax.attributes.html" title="Attributes">attributes</a> passed to
     template functions from the template are contained in the
     <em class="parameter"><code>$params</code></em> as an associative array.
    </p>
<p>
	 The output (return value) of the function will be substituted in place of
     the function tag in the template, eg the
     <a class="link" href="language.function.fetch.html" title="{fetch}"><code class="varname">{fetch}</code></a>
     function. Alternatively, the function can simply perform some other
     task without any output, eg the <a class="link" href="language.function.assign.html" title="{assign}">
     <code class="varname">{assign}</code></a> function.
    </p>
<p>
     If the function needs to assign some variables to the template or use
     some other Smarty-provided functionality, it can use the supplied
     <em class="parameter"><code>$template</code></em> object to do so eg
     <code class="literal">$template-&gt;foo()</code>.
    </p>
<p>
     </p>
<div class="example">
<a name="id463682"></a><p class="title"><b>Example 18.1. function plugin with output</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.eightball.php
 * Type:     function
 * Name:     eightball
 * Purpose:  outputs a random magic answer
 * -------------------------------------------------------------
 */
function smarty_function_eightball($params, Smarty_Internal_Template $template)
{
    $answers = array('Yes',
                     'No',
                     'No way',
                     'Outlook not so good',
                     'Ask again soon',
                     'Maybe in your reality');

    $result = array_rand($answers);
    return $answers[$result];
}
?&gt;

</pre></div>
</div>
<p><br class="example-break">
    </p>
<p>
     which can be used in the template as:
    </p>
<pre class="programlisting">
Question: Will we ever have time travel?
Answer: {eightball}.
    </pre>
<p>
     </p>
<div class="example">
<a name="id463720"></a><p class="title"><b>Example 18.2. function plugin without output</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.assign.php
 * Type:     function
 * Name:     assign
 * Purpose:  assign a value to a template variable
 * -------------------------------------------------------------
 */
function smarty_function_assign($params, Smarty_Internal_Template $template)
{
    if (empty($params['var'])) {
        trigger_error("assign: missing 'var' parameter");
        return;
    }

    if (!in_array('value', array_keys($params))) {
        trigger_error("assign: missing 'value' parameter");
        return;
    }

    $template-&gt;assign($params['var'], $params['value']);     
    
}
?&gt;

      </pre></div>
</div>
<p><br class="example-break">
    </p>
<p>
     See also:
     <a class="link" href="api.register.plugin.html" title="registerPlugin()"><code class="varname">registerPlugin()</code></a>,
     <a class="link" href="api.unregister.plugin.html" title="unregisterPlugin"><code class="varname">unregisterPlugin()</code></a>.
    </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="plugins.writing.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="plugins.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="plugins.modifiers.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Writing Plugins </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Modifiers</td>
</tr>
</table>
</div>
</body>
</html>
